Skip to main content

How to Install MariaDB on Ubuntu 22.04

This guide will help you through the installation of MariaDB on Ubuntu 22.04, providing a robust database solution for your applications and projects.

MariaDB is a powerful open-source relational database management system that originated as a fork of MySQL. It is often preferred as an alternative to MySQL due to its fault tolerance, speed, and scalability.

Prerequisites

  • Launch your Ubuntu 22.04 instance on RackBank.
  • Access the server via SSH.

Update and Upgrade the System

First, make sure your package list is up to date to prevent issues with outdated packages:

sudo apt-get update -y  
sudo apt-get upgrade -y  

Installing MariaDB

To install the MariaDB package, execute the following command:

sudo apt-get install mariadb-server -y  

Start and Enable MariaDB

After the installation completes, start the MariaDB service:

sudo systemctl start mariadb  

Enable the service to start automatically on system boot:

sudo systemctl enable mariadb  

Check the Status

To verify that the MariaDB service is active and running, use:

sudo systemctl status mariadb  

Network Status of the MariaDB Service

You can check the network status of the MariaDB service by executing the following command in the terminal:

sudo ss -antpl | grep mariadb  

Configure MariaDB

It is recommended to run the mysql_secure_installation script to configure your database security settings and access privileges:

sudo mysql_secure_installation  

To secure MariaDB, you will need to use the current credentials for login, including the password for the root user. If this is your first time setting up MariaDB and you haven't created a root password yet, simply press Enter.

When prompted to set the root password or use unix_socket, if you have already secured your root account, you can safely respond with 'n'.

Make sure that the password for your root account is between 8 to 10 characters.

By default, MariaDB includes an anonymous user. If you want to remove this user, press 'Y'; otherwise, press 'n'.

If you wish to disallow remote root login, press 'Y'; otherwise, press Enter.

MariaDB comes with a default database named 'test' that anyone can access. If you want to delete the test database, press 'Y'; otherwise, press 'n'.

Once you've completed these steps, your MariaDB installation should be secure. You can then reopen MariaDB and revert the root user's authentication method to the default, auth_socket. To verify your identity as the main MySQL user with a password, run:

sudo mysql -u root -p  

After executing the command, enter your password when prompted.

Create a MariaDB User

To create a new MariaDB user, enter the following command in the MariaDB console:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password1';  

Grant Privileges to the MariaDB User

Once the new user is created, you can grant them the necessary permissions:

GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password1';  

Creating a MariaDB Database

To create a database in MariaDB, follow these steps:

  1. Log into MariaDB with the command:
mysql -u username -p  

Replace username with your MariaDB username. You will be prompted for your password.

  1. Once logged in, create a new database by executing:
CREATE DATABASE database_name;  

Replace database_name with your desired database name.

To verify that the database was created, use:

SHOW DATABASES;  

Your new database should appear in the list. To create another database, run the following command in your MariaDB console:

CREATE DATABASE database_name;  

User and Database Permissions

After setting up a new user and database, you can grant the necessary permissions:

GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'%';  

Now, review the MariaDB configurations:

sudo mysql -u root -p  

Finally, check the status of the databases by executing:

SHOW DATABASES;